﻿Imports System
Imports System.Collections.Generic
Imports System.Text
''' <summary>
''' Contains the relationship logic rules for adding people and how they relate to each other.
''' </summary>
Public Class RelationshipHelper

    Private Sub New()

    End Sub

    ''' <summary>
    ''' Performs the business logic for adding the Child relationship between the Item and the child.
    ''' </summary>
    Public Shared Sub AddChild(ByVal Tree As ItemWrapperCollection, ByVal Target As ItemWrapper, ByVal child As ItemWrapper)
        ' Add the new child as a sibling to any existing children
        For Each existingSibling In Target.Children
            Tree.AddSibling(existingSibling, child)
        Next

        Select Case Target.Spouses.Count
            ' Single parent, add the child to the Item
            Case 0
                Tree.AddChild(Target, child)
                Exit Select
                ' Has existing spouse, add the child to the Item's spouse as well.
            Case 1
                Tree.AddChild(Target, child)
                Tree.AddChild(Target.Spouses(0), child)
                Exit Select
        End Select
    End Sub


    ''' <summary>
    ''' Performs the business logic for adding the Parent relationship between the Item and the parent.
    ''' </summary>
    Public Shared Sub AddParent(ByVal Tree As ItemWrapperCollection, ByVal Target As ItemWrapper, ByVal parent As ItemWrapper)
        ' A Item can only have 2 parents, do nothing
        If Target.Parents.Count = 2 Then
            Return
        End If
        ' Add the parent to the main collection of people.
        Tree.Add(parent)
        Select Case Target.Parents.Count
            ' No exisitng parents
            Case 0
                Tree.AddChild(parent, Target)
                Exit Select
                ' An existing parent
            Case 1
                Tree.AddChild(parent, Target)
                'Tree.AddSpouse(parent, Target.Parents(0))
                Exit Select
        End Select
        ' Handle siblings
        If (Target.Siblings.Count > 0) Then
            ' Make siblings the child of the new parent
            For Each sibling In Target.Siblings
                Tree.AddChild(parent, sibling)
            Next
        End If
        ' Setter for property change notification
        'Target.HasParents = True
    End Sub

    ''' <summary>
    ''' Performs the business logic for adding the Sibling relationship between the Item and the sibling.
    ''' </summary>
    Public Shared Sub AddSibling(ByVal Tree As ItemWrapperCollection, ByVal Target As ItemWrapper, ByVal sibling As ItemWrapper)
        ' Handle siblings
        If (Target.Siblings.Count > 0) Then
            ' Make the siblings siblings to each other.
            For Each existingSibling In Target.Siblings
                Tree.AddSibling(existingSibling, sibling)
            Next

        End If

        If Not Target.Parents Is Nothing Then
            Select Case Target.Parents.Count
                ' No parents
                Case 0
                    Tree.AddSibling(Target, sibling)
                    Exit Select

                    ' Single parent
                Case 1
                    Tree.AddSibling(Target, sibling)
                    Tree.AddChild(Target.Parents(0), sibling)
                    Exit Select

                    ' 2 parents
                Case 2
                    ' Add the sibling as a child of the same parents
                    For Each parent In Target.Parents
                        Tree.AddChild(parent, sibling)
                    Next

                    Tree.AddSibling(Target, sibling)
                    Exit Select
                Case Else
                    Tree.AddSibling(Target, sibling)
                    Exit Select
            End Select

        End If

    End Sub

    ''' <summary>
    ''' Helper function for removing sibling relationships
    ''' </summary>
    Private Shared Sub RemoveSiblingRelationships(ByVal Target As ItemWrapper)
        For i = Target.Relationships.Count - 1 To 0 Step -1
            If Target.Relationships(i).RelationshipType = RelationshipType.Sibling Then
                Target.Relationships.RemoveAt(i)
            End If
        Next
    End Sub


    ''' <summary>
    ''' Helper function for removing a parent relationship
    ''' </summary>
    Private Shared Sub RemoveParentChildRelationship(ByVal Target As ItemWrapper, ByVal parent As ItemWrapper)
        For Each relationship In Target.Relationships
            If relationship.RelationshipType = RelationshipType.Parent AndAlso relationship.RelationTo.Equals(parent) Then
                Target.Relationships.Remove(relationship)
                Exit For
            End If
        Next


        For Each relationship In parent.Relationships
            If relationship.RelationshipType = RelationshipType.Child AndAlso relationship.RelationTo.Equals(Target) Then
                parent.Relationships.Remove(relationship)
                Exit For
            End If

        Next

    End Sub




    ''' <summary>
    ''' Performs the business logic for changing the deleting the Item
    ''' </summary>
    Public Shared Sub DeleteItem(ByVal Tree As ItemWrapperCollection, ByVal ItemToDelete As ItemWrapper)


        ' Remove the ItemToDelete from the relationships that contains the ItemToDelete.
        For Each relationship In ItemToDelete.Relationships
            For Each rel In relationship.RelationTo.Relationships

                If rel.RelationTo.Equals(ItemToDelete) Then
                    relationship.RelationTo.Relationships.Remove(rel)
                    Exit For
                End If
            Next

            ' TODO: Delete realted bits

            Tree.Remove(ItemToDelete)
        Next

    End Sub

End Class
